package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.service.ContentService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Service
@DubboService
public class ContentServiceImpl implements ContentService {

    @Resource
    private TbContentMapper contentMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<TbContent> getContentPageInfo() {
        return contentMapper.selectByExample(null);
    }

    @Override
    public int contentAdd(TbContent content) {
        return contentMapper.insertSelective(content);
    }

    @Override
    public TbContent toContentUpdate(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public int contentUpdate(TbContent content) {
        return contentMapper.updateByPrimaryKeySelective(content);
    }

    @Override
    public int deleteId(Long id) {
        return contentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int contentAudit(TbContent content) {
        return contentMapper.updateByPrimaryKeySelective(content);
    }

    @Override
    public List<TbContent> getSlideshow() {
        List<TbContent> contentList = null;
        if (redisTemplate.hasKey("getSlideshow")){
            Object o = redisTemplate.opsForValue().get("getSlideshow");
            contentList = JSONObject.parseArray(o.toString(),TbContent.class);
            return contentList;
        }
        contentList = contentMapper.getSlideshow();
        String  tbContentListJson= JSONObject.toJSONString(contentList);
        redisTemplate.opsForValue().set("getSlideshow",tbContentListJson);
        return contentList;
    }

    @Override
    public List<TbContent> getAboutUs() {
        List<TbContent> contentList = null;
        if (redisTemplate.hasKey("getAboutUs")){
            Object o = redisTemplate.opsForValue().get("getAboutUs");
            contentList = JSONObject.parseArray(o.toString(),TbContent.class);
            return contentList;
        }
        contentList = contentMapper.getAboutUs();
        String  tbContentListJson= JSONObject.toJSONString(contentList);
        redisTemplate.opsForValue().set("getAboutUs",tbContentListJson);
        return contentList;
    }

    @Override
    public List<TbContent> getRelated() {
        List<TbContent> contentList = null;
        if (redisTemplate.hasKey("getRelated")){
            Object o = redisTemplate.opsForValue().get("getRelated");
            contentList = JSONObject.parseArray(o.toString(),TbContent.class);
            return contentList;
        }
        contentList = contentMapper.getRelated();
        String  tbContentListJson= JSONObject.toJSONString(contentList);
        redisTemplate.opsForValue().set("getRelated",tbContentListJson);
        return contentList;
    }

    @Override
    public List<TbContent> selectByCategoryId(Long categoryId) {
        List<TbContent> tbContentList=null;
        if (redisTemplate.hasKey(categoryId.toString())){
            Object o = redisTemplate.opsForValue().get(categoryId.toString());
            tbContentList = JSONObject.parseArray(o.toString(),TbContent.class);
            return tbContentList;
        }
        tbContentList = contentMapper.selectList(categoryId);
        String  tbContentListJson= JSONObject.toJSONString(tbContentList);
        redisTemplate.opsForValue().set(categoryId.toString(),tbContentListJson);
        return tbContentList;
    }
}
